// index.js
// 日记聚合页


import util from '../../../utils/util.js';
var app = getApp();

Page({

  data: {
    carlist:[],
    type: 0,
    sortType: 1,
    typestr: "全部分类",
    sortTypestr: "默认排序",
  },

  showDetail: function (obj) {
    var that = this;
    var objkey = parseInt(obj.currentTarget.id);
    var ret = this.data.carlist[objkey];
    let data = JSON.stringify(ret);
    wx.navigateTo({
      url: '../../carstore/detail/detail?data=' + data,
    });
  },
  
  jumpToMyCar() {
    wx.navigateTo({
      url: '../addcar/mycar'
    })
  },

  onShow: function () {
    this.getlist();

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options) {
    var toptype = options.type;
    var that = this;

    if (toptype == 0) {
      that.setData({ typestr: '全部分类' });
    } else if (toptype == 1) {
      that.setData({ typestr: '分类(洗车)' });
    } else if (toptype == 2) {
      that.setData({ typestr: '分类(保养)' });
    } else if (toptype == 3) {
      that.setData({ typestr: '分类(钣金)' });
    } else if (toptype == 4) {
      that.setData({ typestr: '分类(喷漆)' });
    } else if (toptype == 5) {
      that.setData({ typestr: '分类(维修)' });
    }

    that.getlist();
  },

editDetail:function(obj){
  var that = this;
  var objkey = parseInt(obj.currentTarget.id);
  // for()
  var ret = this.data.carlist[objkey];

  // for (var i = 0; i < this.dcarlist.length; i++) {

  //   console.log(this.carlist[i].carcode);

  // }
  let data = JSON.stringify(ret);

    wx.navigateTo({
      url: '../addcar/mycar?data=' + data,
    });

},

  bindtypeTap: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['全部', '洗车', '保养', '钣金', '喷漆', '维修'],
      success: function (res) {
        that.data.type = res.tapIndex;

        if (res.tapIndex == 0) {
          that.setData({ typestr: '全部分类' });
        } else if (res.tapIndex == 1) {
          that.setData({ typestr: '分类(洗车)' });
        } else if (res.tapIndex == 2) {
          that.setData({ typestr: '分类(保养)' });
        } else if (res.tapIndex == 3) {
          that.setData({ typestr: '分类(钣金)' });
        } else if (res.tapIndex == 4) {
          that.setData({ typestr: '分类(喷漆)' });
        } else if (res.tapIndex == 5) {
          that.setData({ typestr: '分类(维修)' });
        }

        that.getlist();
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  bindSorderTap: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['距离最近', '评分最高', '评论最多', '收藏最多'],
      success: function (res) {
        that.data.sortType = res.tapIndex + 1;
        if (res.tapIndex == 0) {
          that.setData({ sortTypestr: '距离最近' });
        } else if (res.tapIndex == 1) {
          that.setData({ sortTypestr: '评分最高' });
        } else if (res.tapIndex == 2) {
          that.setData({ sortTypestr: '订单最多' });
        } else if (res.tapIndex == 3) {
          that.setData({ sortTypestr: '收藏最多' });
        }
        that.getlist();
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  getlist(){
    var url = app.globalData.SERVER+'basedata/getFilteredShops';

    var lon = app.globalData.lon;
    var lat = app.globalData.lan;

    var ptype = this.data.type;
    var psort = this.data.sortType
    
    util.request(url, 'POST', {
      key:'',
      lon: lon,
      lan:lat,
      type: ptype,
      sortType: psort,
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1 ) {
        this.setData({
        carlist: err.data.ret,
        });
        var  num = 1;
      } else {
        self.setData({
          siteInfo: err.data.site_info
        });
      }

     
    });
  }
})
